﻿Public Class frmHire

    Private Sub frmHire_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
    Private Sub frmHire_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked

    End Sub
    Private Sub frmHire_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Icon = Icon.FromHandle(My.Resources.cup.GetHicon)
        camera.Init()
    End Sub
    Private Sub cmdSnap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSnap.Click
        Snap()
    End Sub
    Sub Snap()
        If Not camera.IsReady Then
            My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Exclamation)
            Exit Sub
        End If

        pbxPic.Image = camera.TakePicture
    End Sub
    Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
        'Create something like a random access file for the stranded mode,
        'which is rapidly becoming the standard mode unless Beau brings
        'a server today >:(
        Dim strm As IO.FileStream
        Dim enc As New System.Text.ASCIIEncoding
        Dim r As New IO.MemoryStream()
        Dim arr As Byte()

        Dim spath As String
        Dim sname As String

        Dim i As Integer = 0

        sname = LCase(txtFName.Text.Substring(0, 1) & txtLName.Text)

        'Prevent username collision
        While user_registry.ContainsKey(sname)
            i += 1

            If Not user_registry.ContainsKey(sname & CStr(i)) Then
                sname &= CStr(i)
                Exit While
            End If
        End While

        spath = "id\worker\" & IO.Path.GetRandomFileName & "._"

        'Prevent file name collision
        While IO.File.Exists(spath)
            spath = "id\worker\" & IO.Path.GetRandomFileName & "._"
        End While

        strm = New IO.FileStream(spath, IO.FileMode.Create, IO.FileAccess.Write)
        'Save the picture to the memory stream
        Try
            pbxPic.Image.Save(r, Drawing.Imaging.ImageFormat.Jpeg)
        Catch ex As Exception

        End Try

        'Get the byte array from the memory stream, and force it to be 8,192 bytes in size
        arr = r.ToArray

        ReDim Preserve arr(8192)

        'Write alll the data as bytes to the file

        strm.Write(arr, 0, arr.Length)
        strm.Write(enc.GetBytes(sname.PadRight(33)), 0, 33)
        strm.Write(enc.GetBytes(txtLName.Text.PadRight(txtLName.MaxLength)), 0, txtLName.MaxLength)
        strm.Write(enc.GetBytes(txtFName.Text.PadRight(txtFName.MaxLength)), 0, txtFName.MaxLength)
        strm.Write(enc.GetBytes(txtMI.Text.PadRight(txtMI.MaxLength)), 0, txtMI.MaxLength)
        strm.Write(enc.GetBytes(txtAddress1.Text.PadRight(txtAddress1.MaxLength)), 0, txtAddress1.MaxLength)
        strm.Write(enc.GetBytes(txtAddress2.Text.PadRight(txtAddress2.MaxLength)), 0, txtAddress2.MaxLength)
        strm.Write(enc.GetBytes(txtCity.Text.PadRight(txtCity.MaxLength)), 0, txtCity.MaxLength)
        strm.Write(enc.GetBytes(txtState.Text.PadRight(txtState.MaxLength)), 0, txtState.MaxLength)
        strm.Write(enc.GetBytes(txtZip.Text.PadRight(txtZip.MaxLength)), 0, txtZip.MaxLength)
        strm.Write(enc.GetBytes(txtPhone.Text.PadRight(txtPhone.MaxLength)), 0, txtPhone.MaxLength)
        strm.Write(enc.GetBytes(txtMobile.Text.PadRight(txtMobile.MaxLength)), 0, txtMobile.MaxLength)
        strm.Write(enc.GetBytes(CStr(ACCESS_CAREGIVER)), 0, 1)

        strm.Close()

        IO.File.Encrypt(strm.Name)

        'Save, encrypt, alert of new user, update the user_registry, save the changes, and beep

        MsgBox(String.Format("{0} {1} has been added to the system with the username {2}, password: {3}", Arguments(txtFName.Text, txtLName.Text, sname, Today.Date & sname)), MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Registered")

        user_registry(extend.Base64Sha1(sname)) = extend.Base64Sha1(Today.Date & sname)
        extend.SaveUserData()
        extend.ReloadAll()
        Beep()
    End Sub
End Class